package com.yanqu.road.server.cmd.activity.zhenhuan.cross.thirdProcess;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogZhenHuanThirdOpenBanquet;
import com.yanqu.road.logic.activity.zhenhuan.ZhenHuanThirdProcessConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ZhenHuanProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.zhenhuan.ZhenHuanConfigMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;

@Cmd(code = Protocol.S_ZHENHUAN_THIRD_PROCESS_OPEN_BANQUET_FROM_CROSS, desc = "甄嬛三开宴跨服回包")
public class ZhenHuanThirdProcessOpenBanquetFromCrossCmd extends AbstractCommand {

    private int banquetId;

    private int banquetTimes;

    private int openItemId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ZhenHuanProto.CrossZhenHuanThirdProcessOpenBanquetRespMsg reqMsg = ZhenHuanProto.CrossZhenHuanThirdProcessOpenBanquetRespMsg.parseFrom(packet.getBytes());
        ret = reqMsg.getRet();
        if (reqMsg.hasBanquetId()) {
            banquetId = reqMsg.getBanquetId();
        }
        if (reqMsg.hasBanquetTimes()) {
            banquetTimes = reqMsg.getBanquetTimes();
        }
        openItemId = reqMsg.getOpenItemId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ZhenHuanThirdProcessConfig config = ZhenHuanConfigMgr.getThirdProcessConfig();
        if (config == null) {
            ret = GameErrorCode.E_ZHENHUAN_THIRD_PROCESS_CONFIG_NOT_FOUND;
            return;
        }
        // 拿一下消耗
        Property cost = new Property(openItemId, 1);
        // 取消预扣除
        player.getModule(PreReduceModule.class).restorePreReduce(cost);
        if (ret == 0) {
            player.getModule(CurrencyModule.class).removeCurrency(cost, eLogMoneyType.ZhenHuanThirdProcess, eLogMoneyType.ZhenHuanThirdProcessOpenBanquetCost);

            // 更新下成就
            player.notifyListener(eGamePlayerEventType.ZhenHuanThirdProcessBanquetTimes.getValue(), banquetTimes);

            AutoLogMgr.add(new LogZhenHuanThirdOpenBanquet(config.getActivityId(),player.getUserId(), banquetId, banquetTimes, openItemId));
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        ZhenHuanProto.ZhenHuanThirdProcessOpenBanquetRespMsg.Builder respMsg = ZhenHuanProto.ZhenHuanThirdProcessOpenBanquetRespMsg.newBuilder();
        respMsg.setRet(ret);
        if (ret == 0) {
            respMsg.setBanquetId(banquetId);
        }
        player.sendPacket(Protocol.U_ZHENHUAN_THIRD_PROCESS_OPEN_BANQUET, respMsg);
    }
}
